package com.daocao.ds.activity;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.PayPassSetBean;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.event.SetPayPassSuccessEvent;
import com.daocao.ds.util.MD5;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.util.UI;
import com.daocao.ds.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class TransationSetStepTwoActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_clear)
    ImageView btnClear;
    @BindView(R.id.btn_eye)
    ImageView btnEye;
    @BindView(R.id.edit_pass_confirm)
    EditText editPassConfirm;
    @BindView(R.id.btn_clear_confirm)
    ImageView btnClearConfirm;
    @BindView(R.id.btn_eye_confirm)
    ImageView btnEyeConfirm;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;

    private Unbinder unbiner;
    private boolean isOpen = false;
    private boolean isOpenConfirm = false;
    private String verify_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction_set_two);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("交易密码");
        verify_code = getIntent().getStringExtra("verify_token");
    }

    public void getPayPassSet(String password, String verify_pay_passwd, String verify_code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        PayPassSetBean item = new PayPassSetBean();
        item.pay_passwd = MD5.getMD5str(password);
        item.verify_pay_passwd = MD5.getMD5str(verify_pay_passwd);
        item.ctoken = verify_code;
        loadingDialog.show();
        Api.api_service.getPayPassSet(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    EventBus.getDefault().post(new SetPayPassSuccessEvent());
                    ToastUtil.makeText(this, "设置成功");
                    UserInfoGlobal.setPayPass();
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_clear, R.id.btn_eye, R.id.btn_clear_confirm, R.id.btn_eye_confirm, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_clear:
                editPass.setText("");
                break;
            case R.id.btn_eye:
                int passwordLength1 = editPass.getText().length();
                isOpen = !isOpen;
                if (isOpen) {
                    btnEye.setImageResource(R.mipmap.img_transation_eye_open);
                } else {
                    btnEye.setImageResource(R.mipmap.img_transation_eye_close);
                }
                editPass.setInputType(isOpen ?
                        (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL) :
                        (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD));
                editPass.setSelection(passwordLength1);
                break;
            case R.id.btn_clear_confirm:
                editPassConfirm.setText("");
                break;
            case R.id.btn_eye_confirm:
                int passwordLength = editPassConfirm.getText().length();
                isOpenConfirm = !isOpenConfirm;
                if (isOpenConfirm) {
                    btnEyeConfirm.setImageResource(R.mipmap.img_transation_eye_open);
                } else {
                    btnEyeConfirm.setImageResource(R.mipmap.img_transation_eye_close);
                }
                editPassConfirm.setInputType(isOpenConfirm ?
                        (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL) :
                        (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD));
                editPassConfirm.setSelection(passwordLength);
                break;
            case R.id.btn_submit:
                if (UI.toString(editPass).length() != 6) {
                    ToastUtil.makeText(this, "交易密码为6位自然数");
                    return;
                }
                if (!UI.toString(editPass).equals(UI.toString(editPassConfirm))) {
                    ToastUtil.makeText(this, "重复密码输入不一致");
                    return;
                }
                getPayPassSet(UI.toString(editPass), UI.toString(editPassConfirm), verify_code);
                break;
        }
    }
}
